package com.vhall.component.framework.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 */
public class TypeConverter {

    private TypeConverter() {
        throw new IllegalStateException("Utility class");
    }

    public static String toString(Object value) {
        if (value == null) {
            return null;
        }
        return String.valueOf(value);
    }

    public static Integer toInteger(Object value) {
        if (value == null) {
            return null;
        }
        double d = toDouble(value, null);
        return (int) d;
    }

    public static Long toLong(Object value) {
        if (value == null) {
            return null;
        }
        double d = toDouble(value, null);
        return (long) d;
    }

    public static Double toDouble(Object value, Integer decimal) {
        if (value == null) {
            return null;
        }
        if (decimal != null) {
            return toBigDecimal(value, decimal).doubleValue();
        }
        return Double.valueOf(value.toString());
    }

    public static Float toFloat(Object value, Integer decimal) {
        if (value == null) {
            return null;
        }
        if (decimal != null) {
            return toBigDecimal(value, decimal).floatValue();
        }
        return Float.valueOf(value.toString());
    }

    public static BigDecimal toBigDecimal(Object value, Integer decimal) {
        if (value == null) {
            return null;
        }
        int dv = decimal == null ? 2 : decimal;
        return new BigDecimal(value.toString()).setScale(dv, RoundingMode.HALF_UP);
    }

    public static Date toDate(Object value, String pattern) {
        if (value == null) {
            return null;
        }
        return dateParse(value.toString(), pattern);
    }

    public static Boolean toBoolean(Object value) {
        if (value == null) {
            return false;
        }
        return Boolean.valueOf(value.toString());
    }

    private static final ThreadLocal<SimpleDateFormat> DATA_FORMAT = new ThreadLocal<SimpleDateFormat>() {

        @Override
        public void remove() {
            DATA_FORMAT.remove();
        }

        @Override
        public SimpleDateFormat get() {
            SimpleDateFormat format = super.get();
            if (format == null) {
                format = new SimpleDateFormat();
                set(format);
            }
            return format;
        }
    };

    /**
     * 解析excel中的时间
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date dateParse(String date, String pattern) {
        SimpleDateFormat dateFormat = DATA_FORMAT.get();
        dateFormat.applyPattern(pattern);
        try {
            return dateFormat.parse(date);
        } catch (Exception e) {
            throw new IllegalArgumentException(e.getMessage());
        }
    }

    /**
     * 解析excel中的时间
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat dateFormat = DATA_FORMAT.get();
        dateFormat.applyPattern(pattern);
        try {
            return dateFormat.format(date);
        } catch (Exception e) {
            throw new IllegalArgumentException("Unsupported date type: " + date.toString());
        }
    }

    public static Byte toByte(String value) {
        if (value == null) {
            return null;
        }
        return Byte.valueOf(value);
    }

    public static Short toShort(String value) {
        if (value == null) {
            return null;
        }
        return Short.valueOf(value);
    }

    public static Character toChar(String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        return value.charAt(0);
    }

}
